/**
 * 用户注册
 */
$(function (){
	//init_meet_create1();
});

function init_meet_create1()
{
	$("#btn_create1").click(docreatemeet1);
	
	var param = {changeMonth: true,buttonImageOnly: true,showOn: "button",buttonImage: "images/calendar.png"};
	$("#startdate,#enddate,#regstart,#regend").datepicker(param);
	
	$('#domain').blur(domaincheck);
	$('#language_0,#language_1').click(function(){
		showEn($(this).val() == 2?true : false);
	});
}

function domaincheck()
{
	var url = baseUrl + "/?c=meetcp&a=domaincheck";
	$.post(url, {domain:$('#domain').val()}, function (ret){
		if (ret.status == 0)
			alert(ret.message);
	}, "json");
}


function showEn(display)
{
	if (display)
		$("#en_enname,#en_enunit, #en_enaddress").show();
	else
		$("#en_enname,#en_enunit, #en_enaddress").hide();
}


function docreatemeet1()
{
	var fields = ['mid', 'name','enname', 'domain', 'unit' ,'enunit', 'language', 'startdate', 'enddate' , 
	              'address' ,'enaddress' , 'joinnum' , 'regstart', 'regend' , 'isinvite',
	'email', 'emailuser', 'emailpwd', 'smtpserver'];
	
	var param = {};
	
	// 数据载入
	for(var i = 0; i<fields.length; i++){
		param[fields[i]] = $('#' + fields[i]).val();
	}
	// 特殊字段
	param['language'] = $(':radio[name="language"][checked]').val();
	param['isinvite'] = $(':radio[name="isinvite"][checked]').val();
	//console.log(param['isinvite'] );
	
	var mast_fields = [];
	mast_fields.push(['name','会议名称不能为空！']);
	if (param['language'] == 2)
		mast_fields.push(['enname','会议名称(英文)不能为空！']);

	mast_fields.push(['domain','域名不能为空！']);
	mast_fields.push(['unit','单位不能为空！']);
	if (param['language'] == 2)
		mast_fields.push(['enunit','单位(英文)不能为空！']);

	mast_fields.push(['startdate','开幕日期不能为空！']);
	mast_fields.push(['enddate','闭幕日期不能为空！']);
	
	mast_fields.push(['address','办会地点不能为空！']);
	if (param['language'] == 2)
		mast_fields.push(['enaddress','办会地点(英文)不能为空！']);
	
	mast_fields.push(['joinnum','参会人数不能为空！']);
	

	mast_fields.push(['regstart','注册开始不能为空！']);
	mast_fields.push(['regend','注册结束不能为空！']);

	mast_fields.push(['email','官方电邮不能为空！']);
	mast_fields.push(['emailuser','发件人名称不能为空！']);
	mast_fields.push(['emailpwd','电邮密码不能为空！']);
	mast_fields.push(['smtpserver','电邮SMTP不能为空！']);
	// 必填判断
	for(var i = 0; i<mast_fields.length; i++){
		if (typeof(param[mast_fields[i][0]]) == "undefined" || param[mast_fields[i][0]].length <= 0){
			alert(mast_fields[i][1]);
			return;
		}
	}
	
	var mid = $('#mid').val();
	
	var url = baseUrl + "/?c=meetcp&a=docreate1&mid="+mid;
	$.post(url, param, function (ret){
		if (ret.status == 0)
			alert(ret.message);
		else{
			location.href=baseUrl + "/?c=meetcp&a=create2&mid=" + ret.mid;
		}
	}, "json");	
}

// 设置图片
function setPhoto(mid, url)
{
	if (url == "")
		if (!window.confirm("没有上传，将重置为默认")){
			return false;
		}
	
	var param = {url:url};
	var url = baseUrl + "/?c=meetcp&a=docreate3&mid="+mid;
	$.post(url, param, function (ret){
		if (ret.status == 0)
			alert(ret.message);
		else{
			alert("设置成功！");
			location.href=baseUrl + "/?c=meetcp&a=create3&mid="+mid+"&r="+Math.random();
		}
	}, "json");	
	
	return false;
}

// 导航和左菜单，修改位置
function docreate4_pos(mid, type, key, index)
{
	var param = {"mid":mid, "index":index, "opp":type, "key":key};
	var url = baseUrl + "/?c=meetcp&a=docreate4_pos&mid="+mid;
	
	$.post(url, param, function (ret){
		if (ret.status == 0){
			if (ret.type==0){
				alert(ret.message);
				location.reload();
			}
		}else{
			if (ret.index != ret.newindex){
				exchangeRow_4pos($("#row_" + ret.index), $("#row_" + ret.newindex), 4);
			}
		}
	}, "json");	
}

function exchangeRow_4pos(row1,row2, num){
	var temp = "";
	for (i=1;i<num;i++)
	{
		temp=row1.children("td").eq(i).html();
		row1.children("td").eq(i).html(row2.children("td").eq(i).html());
		row2.children("td").eq(i).html(temp);
	}
}

var _cache_s1_param = {};
var tb_s1_url=null;
// 添加项目,编辑  第一步
function docreatemeet4_s1(mid, key, type, language, defaultval)
{
	var width = 650;
	if (language == 1){// 国内会议
		$(".lan_en").hide();
		width = 330;
		$('#id_win_float_box').css("width", "330px");
	}else{
		$(".lan_en").show();
		$('#id_win_float_box').css("width", "650px");
	}
	
	if (type == 'file'){
		$(".c_content").hide();
	}else if (type == 'article'){
		$(".c_content").show();
	}
	
	_cache_s1_param.mid = mid;
	_cache_s1_param.key = key;
	_cache_s1_param.type = type;
	_cache_s1_param.language = language;
	_cache_s1_param.stype = 'add';
	
	if (typeof(defaultval) != 'undefined'){
		_cache_s1_param.stype = 'edit';
		_cache_s1_param.index = defaultval.index;
		
		// 设置
		$("#c_name").val(defaultval.name);
		$("#c_enname").val(defaultval.enname);
		$("#c_aid_cn").val(defaultval.aid);
		$("#c_aid_en").val(defaultval.enaid);
		if (defaultval.show){
			$("#c_show1").attr("checked","checked");
		}else{
			$("#c_show2").attr("checked","checked");
		}
	}else{// 清空设置
		$("#c_name").val("");
		$("#c_enname").val("");
		$("#c_aid_cn").val(0);
		$("#c_aid_en").val(0);
	}
	
	tb_s1_url = "#TB_inline?height=200&width="+width+"&inlineId=div_create&modal=true";
	tb_show("", tb_s1_url);
}

// 创建项目 第二步
function docreatemeet4_s2()
{
	var name = $('#c_name').val();
	var enname = $("#c_enname").val();
	var show = $(':radio[name="c_show"][checked]').val();
	
	if (name.length==0){
		alert("请输入中文标题");
		return;
	}
	if (enname.length==0 && _cache_s1_param.language==2){
		alert("请输入英文标题");
		return;
	}
	
	if (typeof(show) == "undefined" || show.length == 0){
		alert("请选择状态");
		return;
	}

	var cnaid = $("#c_aid_cn").val();
	var enaid = $("#c_aid_en").val();
	
	var param = {"type":_cache_s1_param.type,"key":_cache_s1_param.key, "name":name, "enname":enname, "show":show, "aid":cnaid, "enaid": enaid};
	var url = baseUrl + "/?c=meetcp&a=docreate4_add&mid="+_cache_s1_param.mid;
	if (_cache_s1_param.stype == 'edit'){
		url = baseUrl + "/?c=meetcp&a=docreate4_edit&mid="+_cache_s1_param.mid;
		param.index = _cache_s1_param.index;
	}
	
	$.post(url, param, function (ret){
		if (ret.status == 0){
			alert(ret.message);
		}else{
			location.reload();
		}
	}, "json");	
}

var _last_c4_s2_lan;
// 打开编辑界面
function edit_c4_s2(str_lan)
{
	var aid = 0;
	_last_c4_s2_lan = str_lan;
	
	if (str_lan == 'cn'){
		aid = $("#c_aid_cn").val();
	}else{
		aid = $("#c_aid_en").val();
	}
	
	tb_remove(true);
	tb_show("", baseUrl + "/?c=meetcp&a=docreate4_kdedit&aid="+aid+"&TB_iframe=true&height=530&width=725&inlineId=div_edit&modal=true");
}

// 编辑器 保存回调
function edit_c4_s2_callback_save(aid, content, lan)
{
	//console.log(aid, content);
	var param = {};
	param.mid = _cache_s1_param.mid;
	param.aid = aid;
	param.content = content;
	
	var url = baseUrl + "/?c=meetcp&a=docreate4_article";
	$.post(url, param, function (ret){
		if (ret['status'] == 1){// 返回成功，保存aid
			if (_last_c4_s2_lan == 'cn'){
				$("#c_aid_cn").val(ret.aid);
			}else{
				$("#c_aid_en").val(ret.aid);
			}
			
			edit_c4_s2_callback_close();
		}
	}, "json");
}

// 编辑器 关闭回调
function edit_c4_s2_callback_close()
{
	tb_remove(true);
	if (tb_s1_url != null){
		tb_show("", tb_s1_url);
	}
}

// 删除
function docreate4_del(mid, key, index)
{
	if (window.confirm("确认删除！") == false){
		return;
	}
	
	var url = baseUrl + "/?c=meetcp&a=docreate4_del";
	var param = {};
	param.mid = mid;
	param.key = key;
	param.index = index;
	
	$.post(url, param, function (ret){
		if (ret['status'] == 1){// 返回成功，保存aid
			location.reload();
		}else{
			alert(ret.message);
		}
	}, "json");
}

// 编辑 调用添加函数
function docreate4_edit_s1(index, mid, key, language)
{
	var arr = arr_menu[index];
	arr['index'] = index;
	
	docreatemeet4_s1(mid, key, arr['type'], language, arr);
}


var _cache_item_param = {};
// 添加注册项,编辑  第一步
function docreatemeet5_s1(mid, type, language, defaultval)
{
	var width = 650;
	var divid = "div_create";

	
	if (type == 'desc'){
		divid = "div_create_desc";
		
		if (language == 1){// 国内会议
			$(".lan_en").hide();
		}else{
			$(".lan_en").show();
		}
		width = 480;
	}else if (type == 'item'){
		divid = "div_create";
			
		if (language == 1){// 国内会议
			$(".lan_en").hide();
			width = 330;
			$('#id_win_float_box').css("width", "330px");
		}else{
			$(".lan_en").show();
			$('#id_win_float_box').css("width", "650px");
		}
	}
	
	_cache_item_param.mid = mid;
	_cache_item_param.type = type;
	_cache_item_param.language = language;
	_cache_item_param.stype = 'add';
	
	if (typeof(defaultval) != 'undefined'){
		_cache_item_param.stype = 'edit';
		_cache_item_param.index = defaultval.index;
		if (type == 'desc'){
			$('#c_desc').val(defaultval.desc);
			$('#c_endesc').val(defaultval.endesc);

			if (defaultval.show)
				$("#c_dshow1").attr("checked","checked");
			else
				$("#c_dshow0").attr("checked","checked");
		}else{
			$("#c_title").val(defaultval.title);
			$('#c_entitle').val(defaultval.entitle);
			$("#c_type").val(defaultval.type);
			if (defaultval.type >= 3 && defaultval.type <= 5){
				docreatemeet5_s2_type($('#c_type').get(0));
			}
			$("#c_value").val(defaultval.value);
			$("#c_envalue").val(defaultval.envalue);
			if (defaultval.show)
				$("#c_show_1").attr("checked","checked");
			else
				$("#c_show_0").attr("checked","checked");
			if (defaultval.required)
				$("#c_required_1").attr("checked","checked");
			else
				$("#c_required_0").attr("checked","checked");
			$("#c_beizhu").val(defaultval.beizhu);
			$("#c_enbeizhu").val(defaultval.enbeizhu);
		}
	}else{// 清空设置
	}
	
	tb_s1_url = "#TB_inline?height=400&width="+width+"&inlineId="+divid+"&modal=true";
	tb_show("", tb_s1_url);
}

// 根据类型处理值是否出现
function docreatemeet5_s2_type(obj)
{
	if (obj.value>=3 && obj.value <= 5){
		$('#type_value').show();
	}else{
		$('#type_value').hide();
	}
}
// 创建第二步
function docreatemeet5_s2()
{
	var title = $("#c_title").val();
	var entitle = $('#c_entitle').val();
	var type = $("#c_type").val();
	var value = $("#c_value").val();
	var envalue = $("#c_envalue").val();
	var show = $(':radio[name="c_show"][checked]').val();
	var required = $(':radio[name="c_required"][checked]').val();
	var beizhu = $("#c_beizhu").val();
	var enbeizhu = $("#c_enbeizhu").val();
	var desc = $("#c_desc").val();
	var endesc = $("#c_endesc").val();
	
	if (_cache_item_param.type == 'desc'){
		show = $(':radio[name="c_dshow"][checked]').val();
		if (desc == ""){
			alert("请填写中文说明！");
			return false;
		}
		
		if (endesc == "" && _cache_item_param.language == 2){
			alert("请填写英文说明！");
			return false;
		}
		
		if (typeof(show) == 'undefined'){
			alert("请选择状态!");
			return false;
		}
		type = 8;
	}
	var enbeizhu = $("#c_enbeizhu").val();
	if (_cache_item_param.type == 'item'){
		if (title == ""){
			alert("请填写中文标题！");
			return false;
		}
		
		if (entitle == "" && _cache_item_param.language == 2){
			alert("请填写英文标题！");
			return false;
		}
		
		if (type == 0){
			alert("请选择类型！");
			return false;
		}
		
		if (typeof(required) == 'undefined'){
			alert("请选择 必填!");
			return false;
		}
		
		if (typeof(show) == 'undefined'){
			alert("请选择状态!");
			return false;
		}
	}
	
	var param = {};
	param.mid = _cache_item_param.mid;
	param.title = title;
	param.entitle = entitle;
	param.type = type;
	param.show = show;
	param.required = required;
	param.beizhu = beizhu;
	param.enbeizhu = enbeizhu;
	param.value = value;
	param.envalue = envalue;
	param.desc = desc;
	param.endesc = endesc;

	var url = baseUrl + "/?c=meetcp&a=create5_add";

	if (_cache_item_param.stype == 'edit'){
		url = baseUrl + "/?c=meetcp&a=create5_add&seq="+_cache_item_param.index;
	}
	
	$.post(url, param, function (ret){
		if (ret.status == 0){
			alert(ret.message);
		}else{
			location.reload();
		}
	}, "json");	
}

// 添加分隔符
function docreatemeet5_s2_t7(mid)
{
	var url = baseUrl + "/?c=meetcp&a=create5_add";
	var param = {};
	param.mid = mid;
	param.type = 7;
	$.post(url, param, function (ret){
		if (ret.status == 0){
			alert(ret.message);
		}else{
			location.reload();
		}
	}, "json");	
}


// 注册字段，修改位置
function docreate5_pos(mid, type, index)
{
	var param = {"mid":mid, "index":index, "opp":type};
	var url = baseUrl + "/?c=meetcp&a=create5_pos&mid="+mid;
	
	$.post(url, param, function (ret){
		if (ret.status == 0){
			if (ret.type==0){
				alert(ret.message);
				location.reload();
			}
		}else{
			if (ret.index != ret.newindex){
				exchangeRow_4pos($("#row_" + ret.index), $("#row_" + ret.newindex), 5);
			}
		}
	}, "json");	
}


//删除
function docreate5_del(mid, index)
{
	if (window.confirm("确认删除！") == false){
		return;
	}
	
	var url = baseUrl + "/?c=meetcp&a=create5_del";
	var param = {};
	param.mid = mid;
	param.index = index;
	
	$.post(url, param, function (ret){
		if (ret['status'] == 1){// 返回成功，保存aid
			location.reload();
		}else{
			alert(ret.message);
		}
	}, "json");
}


// 编辑
function docreate5_edit(mid, index, language)
{
	var item = arr_items[index];
	var type = item.type == 8 ? "desc" : "item";
	
	item['index'] = index;
	docreatemeet5_s1(mid, type, language, item);
}

// 修改状态
function create6_change_status(mid, newstatus)
{
	if (window.confirm("确认发布会议") == false)
		return false;

	var url = baseUrl + "/?c=meetcp&a=cstatus";
	var param = {};
	param.mid = mid;
	param.newstatus = newstatus;
	
	$.post(url, param, function (ret){
		if (ret.status == 0)
			alert(ret.message);
		else{
			alert("发布申请已提交，请等待审核");
			location.href=baseUrl +"/?c=meetcp&a=manage&mid="+mid;
		}
	}, "json");	
	
	return false;
}